﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using DeviceLib;
using Graphic.Interface;
using System.Windows;
using System.Windows.Controls;

namespace Graphic.Engine
{
    class SerialPortEngine : ISerialPort
    {
        private System.IO.Ports.SerialPort transferPort;
        System.Threading.Tasks.Task superviseTask;
        List<BaseControl> deviceList;



        public SerialPortEngine Instance
        {
            get { return instance; }
        }

        private SerialPortEngine()
        {
            superviseTask = new System.Threading.Tasks.Task(Supervise);
            superviseTask.Start();
            transferPort.DataReceived += TransferPort_DataReceived;
        }

        private void TransferPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            throw new NotImplementedException();
        }

        public void ContinuteSupervise()
        {
            throw new NotImplementedException();
        }

        public void InitSuperviseList(List<BaseControl> devices)
        {
            throw new NotImplementedException();
        }

        public void InsertGram(List<BaseControl> deviceList)
        {
            this.deviceList = deviceList;
        }

        public void StopSerial()
        {
            throw new NotImplementedException();
        }

        public void StopSupervise()
        {
            throw new NotImplementedException();
        }

        public void SetPort(SerialPort transPort)
        {
            transferPort = transPort;
        }

        private System.Threading.Thread superviseThread;
        private static SerialPortEngine instance = new SerialPortEngine();

        private void Supervise()
        {
            while(true)
            {
                for(int i  = 0; i < deviceList.Count; i ++)
                {
                    byte[] gram = deviceList[i].GetSuperviseGram();
                    transferPort.Write(gram, 0, gram.Length);
                }
            }

        }
    }
}
